using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace NL.AdreesableAsset
{
    public class AAObj : MonoBehaviour, IInfoWaiting
    {
        void IInfoWaiting.Reset(string _value)
        {
            URL = _value; _Load();
        }


        public string URL;

        public bool KeepInFiirstSobling=false;

        public bool EachEnable = false;

        private void OnEnable()
        {
            if (!_STARTED) return;

            if (EachEnable)
                Reset();
        }
        bool _STARTED = false;
        private void Start()
        {
            Reset();
            _STARTED = true;
        }
        public void Reset()
        {
            if (!gameObject.activeInHierarchy) return;

            StopAllCoroutines();
            StartCoroutine(Reseter());
        }
        IEnumerator Reseter()
        {
            yield return 0;
            _Load();
        }

        GameObject _OB;
        void _Load()
        {
            AA.LOAD<GameObject>(URL, (url, w) =>
            {
                if (w != null)
                {
                    _OB=GameObject.Instantiate(w,transform);
                    _OB.transform.localPosition = Vector3.zero;
                    if (KeepInFiirstSobling)
                    { 
                        _OB.transform.SetAsFirstSibling();
                    }
                }

            },gameObject);
        }
    }

}
